package com.ruoyi.share.service.impl;

import com.ruoyi.data.DataUtils;
import com.ruoyi.share.domain.HouseRateT;
import com.ruoyi.share.mapper.HouseRateTMapper;
import com.ruoyi.share.service.IHouseRateTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 利息分摊Service业务层处理
 * 
 * @author lijie
 * @date 2023-08-04
 */
@Service
public class HouseRateTServiceImpl implements IHouseRateTService
{
    @Autowired
    private HouseRateTMapper houseRateTMapper;

    /**
     * 查询利息分摊
     * 
     * @param rateId 利息分摊主键
     * @return 利息分摊
     */
    @Override
    public HouseRateT selectHouseRateTByRateId(Long rateId)
    {
        return houseRateTMapper.selectHouseRateTByRateId(rateId);
    }

    @Override
    public HouseRateT getRateDetail(Long rateId)
    {
        return houseRateTMapper.getRateDetail(rateId);
    }

    @Override
    public HouseRateT selectHouseMaxRateT()
    {
        return houseRateTMapper.selectHouseMaxRateT();
    }

    /**
     * 查询利息分摊列表
     * 
     * @param houseRateT 利息分摊
     * @return 利息分摊
     */
    @Override
    public List<HouseRateT> selectHouseRateTList(HouseRateT houseRateT)
    {
        return houseRateTMapper.selectHouseRateTList(houseRateT);
    }

    @Override
    public List<HouseRateT> selectHouseRateDetailTByRateId(HouseRateT houseRateT)
    {
        return houseRateTMapper.selectHouseRateDetailTByRateId(houseRateT);
    }

    /**
     * 新增利息分摊
     * 
     * @param houseRateT 第一次利息分摊
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseRateTFirst(HouseRateT houseRateT)
    {
        HouseRateT houseRateVO = new HouseRateT();
        if(houseRateT.getRateDate().before(houseRateT.getCostDate())){
            return 2;
        }
        houseRateVO = houseRateTMapper.getBalanceSumForShare(houseRateT);
        if(houseRateVO!=null){
            List<HouseRateT> houseRateTList = new ArrayList<HouseRateT>();
            houseRateTList = houseRateTMapper.getDoorListForShare(houseRateT);
            Double rateMoney = 0d;
            houseRateVO.setRateType("5");
            houseRateVO.setRateDate(houseRateT.getRateDate());
            houseRateVO.setShouldMoney(houseRateT.getRateMoney());
            houseRateVO.setRemark(houseRateT.getRemark());
            houseRateVO.setCostDate(houseRateT.getCostDate());
            houseRateTMapper.insertHouseRateT(houseRateVO);
            int rateId = houseRateVO.getRateId();
            for(HouseRateT infoVO : houseRateTList){
                infoVO.setTotalInfo(houseRateVO.getTotalInfo());
                if(houseRateVO.getTotalInfo()!=0){
                    infoVO.setRateMoney(DataUtils.doubleScale2(infoVO.getDoorInfo()*houseRateT.getRateMoney()/houseRateVO.getTotalInfo()));
                    infoVO.setRateType("5");
                    infoVO.setRateDate(houseRateT.getRateDate());
                    infoVO.setRateId(rateId);
                    houseRateTMapper.insertMoneyDetail(infoVO);
                    houseRateTMapper.updateDoorRateType(infoVO);
                    rateMoney = DataUtils.sumMoney(rateMoney, DataUtils.doubleScale2(infoVO.getDoorInfo()*houseRateT.getRateMoney()/houseRateVO.getTotalInfo()));
                }

            }
            houseRateVO.setRateMoney(rateMoney);
            houseRateVO.setRateId(rateId);
            houseRateTMapper.updateHouseRateT(houseRateVO);
            return 1;
        }else{
            return 3;
        }

    }

    /**
     * 新增利息分摊
     *
     * @param houseRateT 利息分摊
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseRateT(HouseRateT houseRateT)
    {
        HouseRateT houseRateVO = new HouseRateT();
        HouseRateT houseRateVO1 = new HouseRateT();
        if(houseRateT.getRateDate().before(houseRateT.getCostDate())){
            return 2;
        }
        houseRateVO = houseRateTMapper.getBalanceSumForShareGreaterRateDate(houseRateT);
        houseRateVO1 = houseRateTMapper.getBalanceSumForShareLessRateDate(houseRateT);
        if(houseRateVO == null){
            houseRateVO = new HouseRateT();
            houseRateVO.setTotalInfo(0d);
        }
        if(houseRateVO1==null){
            houseRateVO1 = new HouseRateT();
            houseRateVO1.setTotalInfo(0d);
        }
        List<HouseRateT> houseRateTList = new ArrayList<HouseRateT>();
        List<HouseRateT> houseRateTList1 = new ArrayList<HouseRateT>();
//        houseRateTList = houseRateTMapper.getDoorListForShare(houseRateT);
        houseRateTList = houseRateTMapper.getDoorListForShareGreaterRateDate(houseRateT);
        houseRateTList1 = houseRateTMapper.getDoorListForShareLessRateDate(houseRateT);
        for(HouseRateT rateVO:houseRateTList1){
            houseRateTList.add(rateVO);
        }
        Double rateMoney = 0d;
        houseRateVO.setRateType("5");
        houseRateVO.setRateDate(houseRateT.getRateDate());
        houseRateVO.setShouldMoney(houseRateT.getRateMoney());
        houseRateVO.setRemark(houseRateT.getRemark());
        houseRateVO.setCostDate(houseRateT.getCostDate());
        houseRateVO.setTotalInfo(DataUtils.sumMoney(houseRateVO.getTotalInfo(),houseRateVO1.getTotalInfo()));
        houseRateTMapper.insertHouseRateT(houseRateVO);
        int rateId = houseRateVO.getRateId();
        for(HouseRateT infoVO : houseRateTList){
            infoVO.setTotalInfo(houseRateVO.getTotalInfo());
            if(houseRateVO.getTotalInfo()!=0){
                infoVO.setRateMoney(DataUtils.doubleScale2(infoVO.getDoorInfo()*houseRateT.getRateMoney()/houseRateVO.getTotalInfo()));
                infoVO.setRateType("5");
                infoVO.setRateDate(houseRateT.getRateDate());
                infoVO.setRateId(rateId);
                houseRateTMapper.insertMoneyDetail(infoVO);
                houseRateTMapper.updateDoorRateType(infoVO);
                rateMoney = DataUtils.sumMoney(rateMoney, DataUtils.doubleScale2(infoVO.getDoorInfo()*houseRateT.getRateMoney()/houseRateVO.getTotalInfo()));
            }

        }
        houseRateVO.setRateMoney(rateMoney);
        houseRateVO.setRateId(rateId);
        return houseRateTMapper.updateHouseRateT(houseRateVO);
    }

    /**
     * 修改利息分摊
     * 
     * @param houseRateT 利息分摊
     * @return 结果
     */
    @Override
    public int updateHouseRateT(HouseRateT houseRateT)
    {
        return houseRateTMapper.updateHouseRateT(houseRateT);
    }

    @Override
    public int updateRemarkT(HouseRateT houseRateT)
    {
        return houseRateTMapper.updateRemarkT(houseRateT);
    }

    @Override
    public void updateRateDetailT(HouseRateT houseRateT)
    {
        houseRateTMapper.updateRateDetailT(houseRateT);
    }

    @Override
    public void updateHouseRateTByHouseId(HouseRateT houseRateT)
    {
        houseRateTMapper.updateHouseRateTByHouseId(houseRateT);
    }

    @Override
    public void updateHouseBalanceSumByHouseId(HouseRateT houseRateT)
    {
        houseRateTMapper.updateHouseBalanceSumByHouseId(houseRateT);
    }

    @Override
    public void updateRateSumByRateId(HouseRateT houseRateT)
    {
        houseRateTMapper.updateRateSumByRateId(houseRateT);
    }

    @Override
    public void updateRateSubMoney(HouseRateT houseRateT)
    {
        houseRateTMapper.updateRateSubMoney(houseRateT);
    }

    /**
     * 批量删除利息分摊
     * 
     * @param rateIds 需要删除的利息分摊主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseRateTByRateIds(Integer[] rateIds)
    {
        for(Integer rateId:rateIds){
            houseRateTMapper.deleteHouseRateTByRateId(rateId);
            houseRateTMapper.deleteHouseRateDetailTByRateId(rateId);
        }
        houseRateTMapper.updateHouseRateTByDetail();
        return houseRateTMapper.updateHouseBalanceSum();
    }

}
